/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
// win_filesys.h - windows implementation of virtual file system


#pragma once
#define ZLIB_WINAPI

#include "../zlib/zip.h"
#include "../zlib/unzip.h"

#include <Windows.h>

/*-----------------------------------------------------------------------------
	File system :
-----------------------------------------------------------------------------*/

#define OS_MAX_PATH	MAX_PATH


void	BackSlashesToForwardSlashes(char *string);

class EWinFileSystem;



class EWinZipArchive {
	public:
					EWinZipArchive	( const char *pack_name );
					~EWinZipArchive	( void );
		uint		GetFileNum		( void ) const { return file_list.size(); }
		const char  *GetFileName	( uint n ) const { return file_list[n].c_str(); }
		const char  *GetPackName	( void ) const { return pack_name.c_str(); }
		unzFile		GetZIP			( void ) const { return zip; }

	protected:	
		string			pack_name;
		unzFile			zip;
		std::vector<string>	file_list;
	};



class	EWinFileSystem: public IFileSystem
	{
	public:
								EWinFileSystem	( void );
		virtual					~EWinFileSystem	( void );

		virtual void			UpdatePackInfo	( void );
		
		virtual void			changeDirectory		( const char *path );
		virtual	void			makeDirectory		( const char *path );
		virtual void			removeDirectory		( const char *path );
		virtual void			makePath		( const char *path );
		virtual const char		*getDirectory		( void );

		virtual	EString			startupDir	( void );
		virtual EString			resolvePath	( const EString path );

		virtual bool			isFileExist	( const char *path );
		virtual void			deleteFile	( const char *path );
		virtual const char		*genUniqueName	( const char *path, const char *part, const char *ext );
		
		virtual IHrFile			openFile	( const char *name, FileOpenMode mode );
		virtual	IHrFSSearch		search		( const char *dir, const char *mask, bool ignore_archives=false );
		
	private:
		void		TestFileSystem	( void );
	
		const char	*SearchPath		( const char *name, bool ignore_archives = false );
		bool		FileExistsLocal	( const char *name );
		void		CleanUpArchives	( void );
		IFile		*CreateZipFile	( const char *name );

		uint		ResearchInZip	(char * name);
		uint		ResearchInDisc	(char * dir);

		std::vector<EWinZipArchive*>	zip_list;		
	
		string		init_work_dir;
		string		game_work_dir;
	};



class	EWinFSSearch : public IFSSearch
	{
	public:
							EWinFSSearch	( void );
		virtual				~EWinFSSearch	( void );
		virtual	uint		getItemNum		( void ) const;
		virtual const char	*getItemName	( uint n ) const;

		std::vector<string>		list;		
	};


class	EComFile : public IFile {	
		virtual IPxBlob			binaryBlob	( void );
		virtual IPxBlob			stringBlob	( void );
	};


class	EFile: public EComFile
	{
	friend class EFileSystem;
	public:
								EFile	( const char *name, FileOpenMode mode );
		virtual					~EFile	( void );
								
		virtual const char		*name	( void ) const;
		virtual uint			size	( void ) const;
		virtual FileOpenMode	Mode	( void ) const;
		
		virtual uint			read	( void *buffer, uint size );
		virtual uint			write	( const void *buffer, uint size );
		virtual uint			seek	( int dist, FileSeekMode origin );
		
		virtual void			flush	( void );
		virtual void			printf	( const char *frmt, ... );
		
	private:
		EFile( void );
		
		HANDLE				hFile;
		string				fname;
		FileOpenMode		mode;
	};


class EZipFile: public EComFile
	{
	public:
								EZipFile	( const char *name, EWinZipArchive *pack );
								~EZipFile	( void );
								
		virtual const char		*name	( void ) const;
		virtual uint			size	( void ) const;
		virtual FileOpenMode	Mode	( void ) const;
		
		virtual uint			read	( void *buffer, uint size );
		virtual uint			write	( const void *buffer, uint size );
		virtual uint			seek	( int dist, FileSeekMode origin );
		
		virtual void			flush	( void );
		virtual void			printf	( const char *frmt, ... );

	private:
		EZipFile( void );

		EWinZipArchive		*pack;
		string				fname;
		unz_file_info info;
		std::vector<byte>		buffer;
		int					pointer;
	};

